﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VarMap.Source_Files;
using System.Data;
using System.Runtime.InteropServices;

namespace VarMap
{
    class Program
    {        
        static void Main(string[] args)
        {
            if (args.Count() == 0 || args[0].Equals("/?") || args[0].IndexOf("help", StringComparison.CurrentCultureIgnoreCase) >= 0)
            {
                Console.WriteLine("VarMap.exe /file excelFile /class className /set set [/destPath destPath] [/sheet sheetName1 [sheetName2..]] [/column column1 [column2...]] [/config key1::value1 [key2::value2...]] [/rootAttr attr1::value1 [attr2::value2...]]");
                return;
            }
            string latestOpt = "";
            string file = "";
            string className = "";
            string set = "";
            string path = "";
            string[] tempStr;
            IList<string> sheetNames = new List<string>(); 
            IList<string> columns = new List<string>();
            Dictionary<string, string> configInfo = new Dictionary<string, string>();
            Dictionary<string, string> rootNodeAttributes = new Dictionary<string, string>();

            int paramIndex = 0;           
            while (paramIndex < args.Count())
            {
                if (args[paramIndex].StartsWith(@"/"))
                {
                    latestOpt = args[paramIndex++];
                }              

                switch (latestOpt.ToUpper()) 
                {
                    case "/FILE":
                        file = args[paramIndex++];
                        break;
                    case "/DESTPATH":
                        path = args[paramIndex++];
                        break;
                    case "/CLASS":
                        className = args[paramIndex++];
                        break;
                    case "/SET":
                        set = args[paramIndex++];
                        break;
                    case "/SHEET":
                        sheetNames.Add(args[paramIndex++]);
                        break;
                    case "/COLUMN":
                        columns.Add(args[paramIndex++]);
                        break;
                    case "/CONFIG":
                        tempStr = args[paramIndex++].Split(new string[]{"::"}, StringSplitOptions.None);
                        configInfo.Add(tempStr[0], tempStr[1]);
                        break;
                    case "/ROOTATTR":
                        tempStr = args[paramIndex++].Split(new string[]{"::"}, StringSplitOptions.None);
                        rootNodeAttributes.Add(tempStr[0], tempStr[1]);
                        break;
                    default:
                        Console.WriteLine("Illegal parameters!");
                        Console.WriteLine("VarMapGenerator.exe /file excelFile /class className /set set [/Sheet sheetName1 [sheetName2..]] [/Column column1 [column2 column3...]] [/Config key1::value1 [key2::value2...]] [/rootAttr attr1::value1 [attr2::value2...]]");
                        return;
                }
            }           
           
            if (sheetNames.Count == 0)
            {
                sheetNames.Add("Sheet1");
            }

            if (configInfo.Count == 0)
            {
                configInfo.Add("ManagementServer", "scxopt-ws7-56");
                configInfo.Add("DomainUser", @"scx\scxsvc");
                configInfo.Add("password", "OpsMgr2007R2");
                configInfo.Add("PSMForScoBPAModel", @"C:\ProgramData\Microsoft\Microsoft Baseline Configuration Analyzer 2\Models\SCOrchestratorBPA");
                configInfo.Add("PSM", "LoggingUtility");
                configInfo.Add("Remote", "false");
                configInfo.Add("Query", @"SELECT [ManagementServer], [ClientMachine] FROM [dbo].[CLIENTCONNECTIONS]");
                configInfo.Add("SettingFile", @"C:\Program Files (x86)\Common Files\Microsoft System Center 2012\Orchestrator\Shared\settings.dat");
            }

            if (rootNodeAttributes.Count == 0)
            {
                rootNodeAttributes.Add("xmlns", @"http://smxinfranet/schemas/mcf/2002/01/varmap");
                rootNodeAttributes.Add("assembly", @".\..\Scx.Test.ScoBPA.Functional.dll");
                rootNodeAttributes.Add("dsc", "Description");
                rootNodeAttributes.Add("owner", "owner");
                rootNodeAttributes.Add("contact", "contact");
            }

            VarMap.Source_Files.VarMapGenerator varMap = new Source_Files.VarMapGenerator();           
            DataSet dSet = varMap.GetData(file, sheetNames, columns); 
            for (int i = 0; i < dSet.Tables.Count; i++)
            {
                varMap.ToXML(dSet.Tables[i], path, set, className, rootNodeAttributes, configInfo);
                Console.WriteLine("Create varmap " + dSet.Tables[i].TableName + ".xml successfully");
            }
        }    
    }
}
